<?php
/**
 * @desc 控制器基类
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Controller extends CController
{
	//默认主题
	public $layout='//layouts/base';
	public $breadcrumbs = array();
	//输出数组
	protected $output = array(
			'code' => 300,
			'status' => 0,
			'navTabId' => '',
			'forwardUrl' => '',
			'callbackType' => ''
		);
	//json输出方法
	function renderJson($data){
		if(empty($data['msg'])){
			$errorCode = Yii::app()->params['errorCode'];
			$data['msg'] = $errorCode[$data['status']]['errorMsg'];
		}
        header("Content-type:json/application;charset=utf-8");
		$output = array(
				"statusCode" => $data['code'], 
				"message" => $data['msg'],
				"navTabId" => empty($data['navTabId'])?"":$data['navTabId'], 
				"forwardUrl" => empty($data['forwardUrl'])?"":$data['forwardUrl'],
				"callbackType" => empty($data['callbackType'])?"":$data['callbackType'],
			);
        echo json_encode($output);
		Yii::app()->end();
    }
	//用户登录过滤
	function filterUserLogin($filterChain){
		$model = new LoginForm();
		$result = $model->userIsLogin();
		if(empty($result)){
			if(Yii::app()->controller->id == 'site' && $this->getAction()->getId() == 'index'){
				$this->redirect(Yii::app()->createUrl("site/login"));
				Yii::app()->end();
			}else{
				$data['status'] = 10001;
				$data['code'] = 301;
				$this->renderJson($data);
			}
		}
		$filterChain->run();
	}
	//过滤ajax请求
	function filterAjaxReq($filterChain){
		if(!Yii::app()->request->isAjaxRequest){
			$this->output['status'] = 10002;
			$this->renderJson($this->_output);
		}
		$filterChain->run();
	}
}